package qf.module.mybatis.entity;

import java.util.Arrays;
import java.util.Map;

public class ApiReqInfo {
    // 请求方式
    public static final Integer METHOD_GET = 1;
    public static final Integer METHOD_POST = 2;

    // 参数格式
    public static final String CONTENT_TYPE_JSON = "application/json;charset=utf-8"; // json格式
    public static final String CONTENT_TYPE_XML= "application/xml";  // xml格式

    // 分隔符
    public static final String SPLIT = "#@#";

    private String busiNo; // 业务编号
    private String name; // API名称
    private Integer method; // 请求方式
    private String url; // 请求地址
    private String kv; // 参数固定值
    private String reqTemplate; // 参数模板
    private String proType; // 请求协议
    private String conType; // 参数格式
    private String isAutoToken; //是否需要获取Token处理

    // 从 Redis或DB 获取到AutoToken
    private Map<String,String> autoToken;
    // 从业务中获取到value值
    private String[] values;

    private ApiReqInfo() {
    }

    private ApiReqInfo(String busiNo, String name, Integer method, String url, String kv, String reqTemplate, String proType, String conType) {
        this.busiNo = busiNo;
        this.name = name;
        this.method = method;
        this.url = url;
        this.kv = kv;
        this.reqTemplate = reqTemplate;
        this.proType = proType;
        this.conType = conType;
    }

    public ApiReqInfo(String busiNo) {
        // TODO 这里调用Redis或Dao层获取相应参数
        ApiReqInfo apiReqInfo = mock(busiNo);
        // 初始化数据内容
        this.busiNo = busiNo;
        this.name = apiReqInfo.getName();
        this.method = apiReqInfo.getMethod();
        this.url = apiReqInfo.getUrl();
        this.kv = apiReqInfo.getKv();
        this.reqTemplate = apiReqInfo.getReqTemplate();
        this.proType = apiReqInfo.getProType();
        this.conType = apiReqInfo.getConType();
    }

    private ApiReqInfo mock(String busiNo) {
        ApiReqInfo apiReqInfo = new ApiReqInfo();
        apiReqInfo.busiNo = busiNo;
        apiReqInfo.name = "测试名称";
        apiReqInfo.method = METHOD_GET;
        apiReqInfo.url = "www.baidu.com";
        apiReqInfo.kv = "%s" + SPLIT + "%s";
        apiReqInfo.reqTemplate = "";
        apiReqInfo.proType = "https";
        apiReqInfo.conType = CONTENT_TYPE_JSON;
        return apiReqInfo;
    }

    @Override
    public String toString() {
        return "ApiReqInfo{" +
                "busiNo='" + busiNo + '\'' +
                ", name='" + name + '\'' +
                ", method=" + method +
                ", url='" + url + '\'' +
                ", kv='" + kv + '\'' +
                ", reqTemplate='" + reqTemplate + '\'' +
                ", proType='" + proType + '\'' +
                ", conType='" + conType + '\'' +
                ", isAutoToken='" + isAutoToken + '\'' +
                ", autoToken=" + autoToken +
                ", values=" + Arrays.toString(values) +
                '}';
    }

    public String getBusiNo() {
        return busiNo;
    }

    public String getName() {
        return name;
    }

    public Integer getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    public String getKv() {
        return kv;
    }

    public String getReqTemplate() {
        return reqTemplate;
    }

    public String getProType() {
        return proType;
    }

    public String getConType() {
        return conType;
    }

    public String getIsAutoToken() {
        return isAutoToken;
    }

    public Map<String, String> getAutoToken() {
        return autoToken;
    }

    public String[] getValues() {
        return values;
    }
}
